#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "mydatabase.h"
#include "mytablemodel.h"
#include "myutility.h"
#include "mymodbus.h"
#include "mytables.h"
#include "myprinter/myprinter.h"
#include <QTranslator>
#include <QPixmap>
#include <QQuickView>
#include <QThreadPool>
//#include <QBreakpadHandler.h>
//#include <QBreakpadHttpUploader.h>
#include "modbus/modbus.h"
#include "A39C/qa39c.h"


int main(int argc, char *argv[])
{
//    QBreakpadInstance.setDumpPath("/opt/ShelfScale/bin"); //设置dump生成的路径为crashes
    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
    QApplication a(argc, argv);
    //QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    //QThreadPool::globalInstance()->setMaxThreadCount(10);
    MyDatabase database;
    database.checkOrCreateDatabase();
    qmlRegisterType<myTableModel, 1>("mytablemodel", 1, 0, "MyTableModel");
    //myTableModel* tableModel = new myTableModel();
    MyUtility myUtility;
    engine.rootContext()->setContextProperty("myutility",&myUtility);
    MyModBus myModBus;
        //=new MyModBus();
    mytables myTables(a, engine);
    QTranslator* translator =myTables.translater();
    if( myTables.language() == 1 ) {
        translator->load(":/en_US.qm");
    }
    else if( myTables.language() == 0 ) {
        translator->load(":/zh_CN.qm");
    }
    else if(myTables.language() == 2){
        translator->load(":/indonesia.qm");
    }
    a.installTranslator(translator);

    qRegisterMetaType<RSP_DATAS>("RSP_DATAS");  // 拷贝自豆包：注册自定义类型

//    MyPrinter myPrinter;
//    myPrinter.Printer_init();

//    float fdata[5];
//    fdata[0]= 100;
//    fdata[1]= 10000;
//    fdata[2]= 1200000;
//    fdata[3]= 9;
//    myPrinter.Printer_print_weights(fdata);


    /*测试libmodbus源代码。
    */
//    QA39C a39c;
//    a39c.start();   //执行一次。








    //QSqlDatabase db= database.initialDatabase();
    engine.rootContext()->setContextProperty("mymodbus",&myModBus);
    engine.rootContext()->setContextProperty("mytables",&myTables);
    //engine.rootContext()->setContextProperty("stablemodel",tableModel);
        const QUrl url(QStringLiteral("qrc:/main.qml"));
//        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
//            &a, [url](QObject *obj, const QUrl &objUrl) {
//                if (!obj && url == objUrl)
//                {
//                    //QApplication::exit(-1);
//                    QCoreApplication::exit(-1);
//                }

//            }, Qt::QueuedConnection);
    engine.load(url);
    //viwer.setVisible(false);
    //splash.finish(nullptr);
    if (engine.rootObjects().isEmpty()){
        //db.close();
        database.closeDB();
        //myModBus->close();
        return -1;
    }
    //db.close();
    //database.closeDB();
    //myModBus->close();
    return a.exec();

//    const QUrl url(QStringLiteral("qrc:/main.qml"));
//    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
//        &app, [url](QObject *obj, const QUrl &objUrl) {
//            if (!obj && url == objUrl)
//                QCoreApplication::exit(-1);
//        }, Qt::QueuedConnection);

//    engine.load(url);
//    return app.exec();
}

